<?php
  require_once 'DB.php';
  include_once("./libraries/db.inc.php");
  include_once("./libraries/authentication.inc.php");
  include_once("./queries/jobQueue.inc.php");
  include_once("./queries/jobHistory.inc.php");
  include_once("./queries/machine.inc.php");
  include_once("./libraries/util.inc.php");
  include_once("./libraries/displayUtils.inc.php");

  include_once("./queries/adminJobQueue.inc.php");
  include_once("./queries/adminJobHistory.inc.php");
  include_once("./queries/usagereport.inc.php");
  include_once("./queries/recency.inc.php");

  session_start();
  adminAuthenticate("login.php");

  $connection = DB::connect($dsn, $options);
  if (DB::isError($connection)) {
    trigger_error($connection->getMessage(), E_USER_ERROR);
  }

  printHeader("Main Admin Screen");
  cdbBody();
  cdbSetUp();
  echo "<font size=+3><b><center>",appName," User Interface" .
       "</center></b></font><br />\n"
?>
<!-- ***** BODY GOES FROM HERE UNTIL NEXT COMMENT WITH *'S ***** -->

<!-- for now let's put the job queue info here -->
<?php

  printLink("index.php", "Take me back to the main screen!");
  echo "<br /><br />\n";
 
  //echo time();

  prettyDisplayAggQuery(job_Queue_Global_Summary($connection), 
                        1, 0, "All jobs in queue by status", "Job Status");

  //echo time();
  echo "<br /><p><a href=adminJobQueueSummary.php>Number of users with jobs in the system:</a> " . job_Queue_Global_User_Count($connection) . "<br />\n";
  //echo time();
  //echo "<br /><p>\n"; 

  //echo "<br /><p><a href=adminJobHistorySummary.php>Number of users who have ever run jobs in the system:</a> " . job_History_Global_User_Count($connection) . " (" . job_History_Global_Job_Count($connection) . " jobs.)<br />\n";

  echo "<p><a href=adminJobHistorySummary.php>See users who have ever run jobs in the system</a><br />\n";

  echo "<br />";

//echo time() . "\n";

  echo "<table width=100%>\n";
  echo "<tr><td> <a href=machineReport.php>Machines in the pool right now:</a> " . machine_pool_count($connection) . "</td></tr>\n";

//echo time() . "\n";

  echo "<tr><td> <a href=machineList.php?before_scalar=10&before_unit=minute&after_scalar=1&after_unit=hour>Machines lost in the last hour:</a> " . countLostMachines($connection, "10", "minute", "1", "hour") . "</td></tr>\n";

//echo time() . "\n";

  echo "<tr><td> <a href=machineList.php?before_scalar=1&before_unit=hour&after_scalar=1&after_unit=day>Machines lost in the last day:</a> " . countLostMachines($connection, "1", "hour", "1", "day") . "</td></tr>\n";

//echo time() . "\n";

  echo "<tr><td> <a href=machineList.php?before_scalar=1&before_unit=day&after_scalar=7&after_unit=day>Machines lost in the last week:</a> " . countLostMachines($connection, "1", "day", "7", "day") . "</td></tr>\n";

//echo time() . "\n";

  echo "<tr><td> <a href=machineList.php?before_scalar=7&before_unit=day&after_scalar=1&after_unit=month>Machines lost in the last month:</a> " . countLostMachines($connection, "7", "day", "1",  "month") . "</td></tr>\n";

//echo time() . "\n";

  echo "<tr><td> <a href=machineList.php?before_scalar=1&before_unit=month>Machines lost more than 1 month ago:</a> " . countLostMachines($connection, "1", "month", null, null) . "</td></tr>\n";

  echo "</table>";

	//echo "<br /><p><a href=usagereport.php>Estimated total hours: </a>" . estimateTotalHours($connection) . "<br /><br /><p>\n";

  echo "<br /> Except for the data sources listed "; 
  printLink("exceptionsources.php", "here");

  echo ", all data sources have updated since " . leastRecency($connection);
 
  echo ". The least recent data source is " . leastRecentSource($connection);

  echo ". The most recent data source is " . mostRecentSource($connection) . " and its last update time is " . mostRecency($connection) . "<br /><p>\n";

?>


<!-- ***** END OF BODY ***** -->
<?php
  cdbFinishPage();
  $connection->disconnect();
?>
